package com.example.tbs.common;

import cn.hutool.core.util.PageUtil;
import com.fasterxml.jackson.annotation.JsonInclude;

import lombok.Setter;
import org.codehaus.jackson.annotate.JsonIgnore;

import java.io.Serializable;
import java.util.List;

/**
 * @author qujian
 * @version 1.0
 * @date 2018/10/11
 * @time 15:26
 * @email qj9498@gmail.com
 * @qq 56069397
 */

//@JsonSerialize(include = JsonSerialize.Inclusion.NON_NULL)//Json序列化的时候忽略空的字段
@JsonInclude(JsonInclude.Include.NON_NULL)
@Setter
public class ServerResponse<T> implements Serializable {

    private int code;
    private String msg;
    private T data;

    public ServerResponse() {
    }

    private ServerResponse(int status) {
        this.code = status;
    }

    private ServerResponse(int status, String msg) {
        this.code = status;
        this.msg = msg;
    }

    private ServerResponse(int status, String msg, T data) {
        this.code = status;
        this.msg = msg;
        this.data = data;
    }

    private ServerResponse(int status, T data) {
        this.code = status;
        this.data = data;
    }

    @JsonIgnore     //这个注解表示在json序列化的时候不显示
    public boolean isSuccess() {
        return this.code == ResponseCode.SUCCESS.getCode();
    }

    public int getCode() {
        return code;
    }

    public String getMsg() {
        return msg;
    }

    public T getData() {
        return data;
    }

    public static <T> ServerResponse<T> createBySuccess() {
        return new ServerResponse<T>(ResponseCode.SUCCESS.getCode());
    }

    public static <T> ServerResponse<T> createBySuccessMessage(String message) {
        return new ServerResponse<T>(ResponseCode.SUCCESS.getCode(), message);
    }

    public static <T> ServerResponse<T> createBySuccess(T data) {
        return new ServerResponse<T>(ResponseCode.SUCCESS.getCode(), data);
    }

    public static <T> ServerResponse<T> createBySuccess(String message, T data) {
        return new ServerResponse<T>(ResponseCode.SUCCESS.getCode(), message, data);
    }


    public static <T> ServerResponse<T> createByError() {
        return new ServerResponse<T>(ResponseCode.ERROR.getCode(), ResponseCode.ERROR.getDesc());
    }
    public static <T> ServerResponse<T> createByNokeyError() {
        return new ServerResponse<T>(ResponseCode.NO_DATA.getCode(), "Key:phoneNumber");
    }
    public static <T> ServerResponse<T> createByNoDataError() {
        return new ServerResponse<T>(ResponseCode.NO_DATA.getCode(), "暂无数据:手机号不能为空");
    }
    public static <T> ServerResponse<T> createByNoUserError() {
        return new ServerResponse<T>(ResponseCode.NO_DATA.getCode(), "用户不存在");
    }


    public static <T> ServerResponse<T> createByErrorMessage(String errorMessage) {
        return new ServerResponse<T>(ResponseCode.ERROR.getCode(), errorMessage);
    }

    public static <T> ServerResponse<T> createByBusinessErrorMessage(String errorMessage) {
        return new ServerResponse<T>(ResponseCode.BUSINESS_ERROR.getCode(), errorMessage);
    }

    public static <T> ServerResponse<T> createByErrorCodeMessage(int errorCode, String errorMessage) {
        return new ServerResponse<T>(errorCode, errorMessage);
    }


}
